<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    /**
     * @var string
     */
    protected $parentColumn = 'parent_id';

    /**
     * @var array
     */
    protected $fillable = [
        'parent_id', 'title', 'description', 'sort'
    ];

    /**
     * Get children of current node.
     * @author Vance
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(static::class, $this->parentColumn);
    }

    /**
     * Get parent of current node.
     * @author Vance
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(static::class, $this->parentColumn);
    }

    /**
     * @author Vance
     *
     * @param $query
     * @param $title
     * @return mixed
     */
    public function scopeTitle($query, $title)
    {
        return $query->where(function ($q) use ($title) {
            $q->where('title', $title);
        });
    }
}
